package com.example.common.util;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import java.util.Map;

public class GsonUtil {
    public static Map<String, Object> toMap(Object o) {
        Gson gson = new Gson();
        String json = gson.toJson(o);
        return gson.fromJson(json, new TypeToken<Map<String, Object>>() {
        }.getType());
    }

    public static <T> T toObject(Map<String, Object> map, Class<T> tClass){
        Gson gson = new Gson();
        String json = gson.toJson(map);
        return gson.fromJson(json, tClass);
    }

    public static String toString(Object o){
        Gson gson = new Gson();
        return gson.toJson(o);
    }

    public static JsonObject toJsonObject(String json){
        JsonElement jsonElement = JsonParser.parseString(json);
        return jsonElement.getAsJsonObject();
    }

}
